﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;


namespace JYH.Framework
{
    public  class WindowService
    {
        //string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe";
        //string serviceName = "MyService";


        //判断服务是否存在
        public static bool IsServiceExisted(string serviceName,string serviceFilePath)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        public static ServiceController GetServiceController(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return sc;
                }
            }
            return null;
        }
        //安装服务
        public static void InstallService(string  serviceName, string serviceFilePath)
        {
            if (IsServiceExisted(serviceName,serviceFilePath))
            {
                UninstallService(serviceName,serviceFilePath);    
            }
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }


        }

        //卸载服务
        public static void UninstallService( string serviceName, string serviceFilePath)
        {
            if (IsServiceExisted(serviceName,serviceFilePath))
            {
                ServiceStop(serviceName,serviceFilePath);
                using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = serviceFilePath;
                installer.Uninstall(null);
            }
            }
        }
        //启动服务
        public static void ServiceStart(string serviceName, string serviceFilePath)
        {
            if (IsServiceExisted(serviceName,serviceFilePath))
            {
                using (ServiceController control = new ServiceController(serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Stopped)
                    {
                        control.Start();
                    }
                }
            }
        }

        //停止服务
        public static void ServiceStop(string serviceName, string serviceFilePath)
        {
            if (IsServiceExisted(serviceName,serviceFilePath))
            {
                using (ServiceController control = new ServiceController(serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Running)
                    {
                        control.Stop();
                    }
                }
            }
        }

    }
}
